package com.hertz.alarm.init;

import java.io.IOException;

import javax.annotation.Resource;


import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.hertz.alarm.entity.Team;
import com.hertz.alarm.service.EntityService;
@Service
public class InitNbaTeam {
	
	@Resource
	private EntityService entityService;
	
	public void init(){
		String logo = "http://i0.sinaimg.cn/ty/2011hqj/2013nba/nbaimg/%s.png";
		String teamList = "http://data.sports.sohu.com/nba/json/ajax.php?_action=get_team_list";
		Connection connection = Jsoup.connect(teamList);
		connection.parser(Parser.xmlParser());
		try {
			Document doc = connection.get();
			Elements teamIds = doc.select("root > team > team_id");
			Elements teamNames = doc.select("root > team > team_name_cn");
			for (int i = 0; i < teamIds.size(); i++) {
				String teamId = teamIds.get(i).text();
				String teamName = teamNames.get(i).text();
				String teamLogo = String.format(logo, teamId);
				String alias = teamName;
				byte type = 1;
				byte league = 1;
				Team team = new Team();
				team.setAlias(alias);
				team.setCtime(System.currentTimeMillis());
				team.setLeague(league);
				team.setLogo(teamLogo);
				team.setTeamName(teamName);
				team.setType(type);
				team.setSourceId(teamId);
				entityService.save(team);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
